<?php

/* ----------------------------------------------
  Caloris: Main file

  $Id: index.php 37 2012-10-02 13:47:04Z peekay2005@gmail.com $
  $Author: peekay2005@gmail.com $

  Installation + Main file
  ---------------------------------------------- */

session_start();
SetLocale(LC_ALL, "cs_CZ");


require_once('caloris.php');

//  Settings (from ini file)
loadConstants(__DIR__ . '/cal-components/data/settings.xml');
loadConstants(__DIR__ . '/cal-components/data/modules.xml');

// TODO CALORIS: Verify existence of important files and functions here

if (_CALSET_BASIC_STATE == 'install') {
    header("location: cal-components/modules/administrator/install.php");
    exit();
}

require_once(__DIR__ . '/extensions.php');
require_once(__DIR__ . '/nette.php');

$classScan = scandir(_CALSET_PATHS_BASE . _CALSET_DIRS_CLASSES);

// Auto include classes

for ($a = 0; $a < count($classScan); $a++) {
    if ($classScan[$a] === '.' || $classScan[$a] === '..') {
        continue;
    } else {
        require_once(_CALSET_PATHS_BASE . _CALSET_DIRS_CLASSES . '/' . $classScan[$a]);
    }
}

use Nette\Diagnostics\Debugger;

//Debugger::enable(Debugger::PRODUCTION);

//Debugger::enable(Debugger::DEVELOPMENT);
error_reporting(E_ALL ^ E_NOTICE);

// TODO CALORIS: Simple down

if (strpos($_SERVER["PHP_SELF"], "/cal-components") !== false) {
    $presentation = 0;
} elseif (strpos($_SERVER["PHP_SELF"], "/cal-core") !== false) {
    $presentation = 0;
} elseif (strpos($_SERVER["PHP_SELF"], "/cal-bin") !== false) {
    $presentation = 0;
} else {
    $presentation = 1;
}

if (isset($_SERVER['REDIRECT_URL'])) {
    $page = $_SERVER["REDIRECT_URL"];
} else {
    $page = '';
}

$site = substr($page, strpos($_SERVER["SCRIPT_NAME"], "index.php"));

if (substr($site, -1) == '/') {
    $site = substr($site, 0, -1);
}

if ($site == 'administrator' or $site == 'admin') {
    header("Location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/administrator/index.php");
    exit();
}

// Checks mod-rewrite
if ($presentation == 1) {

    // Cache

    // Delete cache with greater than set size
    $dirscan = scandir(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE);
    $filesize = 0;

    for ($a = 0; $a < count($dirscan); $a++) {
        if ($dirscan[$a] === '.' || $dirscan[$a] === '..') {
            continue;
        } else {
            $filesize += filesize(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE . '/' . $dirscan[$a]);
        }
    }

    if ($filesize > _CALSET_BASIC_CACHE_MAX_SIZE) {
        for ($a = 0; $a < count($dirscan); $a++) {
            if ($dirscan[$a] === '.' || $dirscan[$a] === '..') {
                continue;
            } else {
                unlink(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE . '/' . $dirscan[$a]);
            }
        }
    }

    // Cache Control
    if (_CALSET_BASIC_CACHE == 1) {
        if ($_SERVER["REQUEST_URI"] == '/') {
            $cachefile = 'index';
        } else {
            $cachefile = Nette\Utils\Strings::webalize($_SERVER["REQUEST_URI"]);
        }

        $storage = new Nette\Caching\Storages\FileStorage(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE);
        $cache = new Nette\Caching\Cache($storage);
        $value = $cache->load($cachefile);

        if ($value === NULL) {
            $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
            $xml = simplexml_load_file($file);

            $tmpl = new Caloris\Template;
            $tmpl->input($site, 'xml');
            $tmpl->renderHead();

            $cache->save($cachefile, $tmpl->renderBody($xml->basic->template), array(
                Nette\Caching\Cache::EXPIRE => '+ ' . _CALSET_BASIC_CACHE_TIME . ' minutes',
            ));
            
            echo $tmpl->renderBody($xml->basic->template);
        } else {
            echo $value;
        }
    } else {
        // Non-caching method
        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
        $xml = simplexml_load_file($file);

        $tmpl = new Caloris\Template;
        $tmpl->input($site, 'xml');
        $tmpl->renderHead();
        echo $tmpl->renderBody($xml->basic->template);
    }
}

// Funkce pro XML nastavení do PHP konstant
function loadConstants($file, $define = '_CALSET_') {
    $xml = simplexml_load_file($file);

    foreach ($xml->children() as $child) {

        if (count($child->children()) == 0) {
            define($define . strtoupper($child->getName()), $child);
        }

        foreach ($child->children() as $childofchild) {

            if (count($childofchild->children()) > 0) {
                foreach ($childofchild->children() as $childofchildofchild) {
                    define($define . strtoupper($child->getName()) . "_" . strtoupper($childofchild->getName()) . "_" . strtoupper($childofchildofchild->getName()), $childofchildofchild);
                }
            } else {
                define($define . strtoupper($child->getName()) . "_" . strtoupper($childofchild->getName()), $childofchild);
            }
        }
    }
}